package com.oracle.coherence.patterns.security.accesscontrol;

import com.tangosol.net.CacheFactory;

import javax.security.auth.Subject;
import java.security.Permission;
import java.security.Principal;

/**
 * An implementation of a {@link PermissionsChecker} that does
 * nothing except log the permission request to the Coherence log.
 * <p/>
 *
 * @author Jonathan Knight
 */
public class LoggingPermissionsChecker implements PermissionsChecker {

    public LoggingPermissionsChecker() {
    }

    public void checkPermission(Permission permission, Subject subject) {
        if (CacheFactory.isLogEnabled(CacheFactory.LOG_DEBUG)) {
            StringBuilder msg = new StringBuilder("checkPermission permission=")
                    .append(permission)
                    .append(" subject={ ");
            if (subject != null) {
                for (Principal principal : subject.getPrincipals()) {
                    msg.append(principal).append(' ');
                }
            } else {
                msg.append("null ");
            }

            msg.append('}');

            CacheFactory.log(msg.toString(), CacheFactory.LOG_DEBUG);
        }
    }
}
